//
//  ProfileXMLParser.m
//  MathMasterMain
//
//  Created by Jimmy Hahm on 10-03-29.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ProfileXMLParser.h"
#import "Profile.h"
#import "Score.h"

NSString* const PROFILES = @"Profiles";
NSString* const PROFILE = @"Profile";
NSString* const GRADE= @"Grade";
NSString* const NAME = @"Name";
NSString* const SCORE = @"Score";
NSString* const TYPE = @"Type";
NSString* const S1 = @"S1";
NSString* const S2 = @"S2";
NSString* const S3 = @"S3";
NSString* const S4 = @"S4";
NSString* const S5 = @"S5";

@implementation ProfileXMLParser
- (void) parser: (NSXMLParser*) parser 
didStartElement: (NSString*) elementName 
   namespaceURI: (NSString*) namespaceURI 
  qualifiedName: (NSString*) qualifiedName 
	 attributes: (NSDictionary*) attributeDict
{
    if ( TRUE == [ elementName isEqualToString: PROFILE] )
    {
		Profile* Q = [ Profile new ];
		[ _elementStack addObject: Q ];
    }
	else if ( TRUE == [ elementName isEqualToString: SCORE ] )
    {
		Score* score = [ Score new ];
		Profile* profile = (Profile*)[ _elementStack lastObject ];
		[ profile setScoredb:score];
		
		[ _elementStack addObject: score ];
    }
	else if (TRUE == [ elementName isEqualToString: NAME ] ||
			 TRUE == [ elementName isEqualToString: TYPE ] ||
			 TRUE == [ elementName isEqualToString: GRADE ] ||
			 TRUE == [ elementName isEqualToString: S1 ] ||
			 TRUE == [ elementName isEqualToString: S2 ] ||
			 TRUE == [ elementName isEqualToString: S3 ] ||
			 TRUE == [ elementName isEqualToString: S4 ] ||
			 TRUE == [ elementName isEqualToString: S5 ])
    {
		NSString* element = [ NSString stringWithString: elementName ];
		[ _elementStack addObject: element ];
    }
}

- (void) parser: (NSXMLParser*) parser
foundCharacters: (NSString*) string
{
    NSCharacterSet* characterSet = [ NSCharacterSet 
									whitespaceAndNewlineCharacterSet ];
    NSString* trimmedValue = [ string stringByTrimmingCharactersInSet:
							  characterSet ];
    if ( 0 == [ trimmedValue length ] )
    {
		return;
    }
	
    id element = [ _elementStack lastObject ];
    if ( TRUE == [ element isKindOfClass: [ NSString class ] ] )
    {
		NSUInteger count = [ _elementStack count ];
		NSUInteger index = count - 2;
		id parentElement = [ _elementStack objectAtIndex: index ];
		
		if ( TRUE == [ element isEqualToString: NAME ] )
		{
			Profile* Q = (Profile*)parentElement;
			[ Q	setName: trimmedValue ];
		}
		else if (TRUE == [ element isEqualToString: GRADE])
		{
			Profile* Q = (Profile*)parentElement;
			[ Q	setGrade: trimmedValue ];
		}
		else if (TRUE == [ element isEqualToString: S1])
		{
			Score* score = (Score*)parentElement;
		//	[ score setScore1: (int) trimmedValue ];

		}
	}
}

- (void) parser: (NSXMLParser*) parser 
  didEndElement: (NSString*) elementName
   namespaceURI: (NSString*) namespaceURI 
  qualifiedName: (NSString*) qName
{
    if ( TRUE == [ elementName isEqualToString: PROFILE ] )
    {
		Profile* Q = (Profile*)[ _elementStack lastObject ];
		[ _Q addObject: Q ];
		[ _elementStack removeLastObject ];
    }
    else if (TRUE == [ elementName isEqualToString: NAME ] ||
			 TRUE == [ elementName isEqualToString: SCORE ] ||

			 TRUE == [ elementName isEqualToString: TYPE ] ||
			 TRUE == [ elementName isEqualToString: GRADE ] ||
			 TRUE == [ elementName isEqualToString: S1 ] ||
			 TRUE == [ elementName isEqualToString: S2 ] ||
			 TRUE == [ elementName isEqualToString: S3 ] ||
			 TRUE == [ elementName isEqualToString: S4 ] ||
			 TRUE == [ elementName isEqualToString: S5 ])
    {
		[ _elementStack removeLastObject ];
    }
}

- (NSMutableArray*) parseContentOfUrl: (NSURL*) url //parse xml from url
{
    NSXMLParser* xmlParser = [ [ NSXMLParser alloc ] 
							  initWithContentsOfURL: url ];
    [ xmlParser setDelegate: self ];
	
    _Q = [ NSMutableArray arrayWithCapacity: 1024 ];
    _elementStack = [ NSMutableArray arrayWithCapacity: 1024 ];
	
    [ xmlParser parse ];
	
    return _Q;
}

- (NSMutableArray*) parseContentOfLocal: (NSString*) filename//parse xml file in resource group
{
	NSXMLParser *xmlnameparser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename]]];
	[ xmlnameparser setDelegate: self ];
	
    _Q = [ NSMutableArray arrayWithCapacity: 1024 ];
    _elementStack = [ NSMutableArray arrayWithCapacity: 1024 ];
	
    [ xmlnameparser parse ];
	
    return _Q;
}

- (NSMutableArray*) parseContentOfpath: (NSString*) path //parse xml from a file path
{
	NSXMLParser *xmlnameparser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:path]];
	[ xmlnameparser setDelegate: self ];
	
    _Q = [ NSMutableArray arrayWithCapacity: 1024 ];
    _elementStack = [ NSMutableArray arrayWithCapacity: 1024 ];
	
    [ xmlnameparser parse ];
	
    return _Q;
}


@end
